package com.huluobo.a2201mvvm_imdemo.demo;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.AnticipateInterpolator;
import android.view.animation.BounceInterpolator;
import android.view.animation.CycleInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.LinearInterpolator;
import android.view.animation.OvershootInterpolator;

import com.blankj.utilcode.util.ToastUtils;
import com.huluobo.a2201mvvm_imdemo.MainActivity;
import com.huluobo.a2201mvvm_imdemo.R;
import com.huluobo.a2201mvvm_imdemo.databinding.ActivityAnimBinding;
import com.huluobo.a2201mvvm_imdemo.mvvm.BaseActivity;

public class AnimActivity extends BaseActivity<ActivityAnimBinding> {

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {
        Animation animationTranslate = AnimationUtils.loadAnimation(this, R.anim.anim_tranlate);
        Animation animationRotate = AnimationUtils.loadAnimation(this, R.anim.anim_rotate);
        Animation animationScale = AnimationUtils.loadAnimation(this, R.anim.anim_scale);
        Animation animationAlpha = AnimationUtils.loadAnimation(this, R.anim.anim_alpha);
        Animation animationSet = AnimationUtils.loadAnimation(this, R.anim.anim_set);
        animationSet.setRepeatCount(10);
        animationSet.setRepeatMode(Animation.RESTART);

        dataBinding.translate.setOnClickListener(v -> {
            dataBinding.imageView.startAnimation(animationTranslate);
        });

        dataBinding.rotate.setOnClickListener(v -> {
            dataBinding.imageView.startAnimation(animationRotate);
        });

        dataBinding.scale.setOnClickListener(v -> {
            dataBinding.imageView.startAnimation(animationScale);
        });

        dataBinding.alpha.setOnClickListener(v -> {
            dataBinding.imageView.startAnimation(animationAlpha);
        });

        dataBinding.set.setOnClickListener(v -> {
            dataBinding.imageView.startAnimation(animationSet);
        });

        //帧动画
        AnimationDrawable animationDrawable = (AnimationDrawable) dataBinding.progressIv.getDrawable();
        dataBinding.progressBtn.setOnClickListener(v -> {
            animationDrawable.start();
        });

        dataBinding.valueAnimatorBtn.setOnClickListener(v -> {
//            valueAnim();
//            objectAnim();
            interpolator();
        });

        dataBinding.demoMyView.setOnAniEndListener(() -> {
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
        });
    }

    /**
     * 属性动画的方法 值的属性动画
     */
    public void valueAnim() {
        ValueAnimator valueAnimator = ValueAnimator.ofFloat(1f, 0.1f);
        valueAnimator.setDuration(5000);//设置持续时间
        valueAnimator.setRepeatMode(ValueAnimator.REVERSE);//设置动画重复模式
        valueAnimator.setRepeatCount(ValueAnimator.INFINITE);//设置动画重复次数
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(@NonNull ValueAnimator animation) {
                float currentValue = (float) animation.getAnimatedValue();
                Log.i("TagA", "属性动画监听器中的值:" + currentValue);
                //把上方Value中的值赋值给一个view的透明度
                dataBinding.imageView.setAlpha(currentValue);
                dataBinding.imageView.setScaleX(currentValue);
                dataBinding.imageView.setScaleY(currentValue);
                dataBinding.imageView.setRotation(currentValue);
            }
        });
        valueAnimator.start();
    }

    /**
     * 属性动画, 对象的属性动画
     */
    public void objectAnim() {
        //透明度动画 alpha
        //平移动画 translationX translationY
        //缩放动画 scaleX scaleY
        //旋转 rotation
//        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(dataBinding.imageView, "scaleX", 0f, 360f);
//        objectAnimator.setDuration(5000);
//        objectAnimator.setRepeatCount(ValueAnimator.INFINITE);
//        objectAnimator.setRepeatMode(ValueAnimator.REVERSE);
//        objectAnimator.start();

        ObjectAnimator rotate = ObjectAnimator.ofFloat(dataBinding.imageView, "rotation", 0, 360);
        ObjectAnimator translateX = ObjectAnimator.ofFloat(dataBinding.imageView, "translationX", -100, 100);
        ObjectAnimator translateY = ObjectAnimator.ofFloat(dataBinding.imageView, "translationY", -100, 100);
        ObjectAnimator scaleX = ObjectAnimator.ofFloat(dataBinding.imageView, "scaleX", 1, 2);
        ObjectAnimator scaleY = ObjectAnimator.ofFloat(dataBinding.imageView, "scaleY", 1, 2);
        ObjectAnimator alpha = ObjectAnimator.ofFloat(dataBinding.imageView, "alpha", 1, 0, 1);

        //1 scanleY 旋转+宽拉长+透明 平移XY
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.play(rotate)//3  要执行的效果,必须要有个play
                .with(alpha)//3 与play同时执行
                .with(scaleX)//3 与play同时执行
                .after(scaleY) //1 执行完再执行play
                .after(2000) //2 将play演示2秒后执行, 也就是执行完2秒,再执行play
                .before(translateX)//4 执行完play,再执行
                .before(translateY);//4 执行完play,再执行

        animatorSet.setDuration(10000);
        animatorSet.start();
    }

    public void interpolator() {
        ObjectAnimator translateX = ObjectAnimator.ofFloat(dataBinding.imageView, "translationY", -400, 1000);
        //给动画添加插值器
//        translateX.setInterpolator(new LinearInterpolator());//线性插值器 匀速直线运动
//        translateX.setInterpolator(new AccelerateInterpolator());//加速插值器 开始慢,慢慢加速到匀速
//        translateX.setInterpolator(new DecelerateInterpolator());//减速插值器 开始匀速,慢慢减速
//        translateX.setInterpolator(new AccelerateDecelerateInterpolator());//加速减速插值器 开始慢,中间匀速,慢慢减速
//        translateX.setInterpolator(new AnticipateInterpolator());//反向加速插值器 先后退一段距离,然后再加速
//        translateX.setInterpolator(new BounceInterpolator());//弹球落地效果;
//        translateX.setInterpolator(new CycleInterpolator(2));//循环插值器,会反方向走相同的距离,再走到终点,目前会循环2遍
//        translateX.setInterpolator(new OvershootInterpolator());//超越插值器 超过终点一定距离,再返回终点

        translateX.setDuration(5000);
        translateX.setRepeatMode(ValueAnimator.RESTART);
        translateX.setRepeatCount(-1);
        translateX.start();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_anim;
    }
}